package com.lwl.blue.commont.utils.ancDec;

import com.lwl.blue.commont.config.mybatis.SensitiveField;
import com.lwl.blue.commont.utils.ancDec.EncryptUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * <p>  </p>
 *
 * @Date: 2021/11/22 10:08
 * @version: V1.0
 * @Decription:
 * @author: 蓝文良
 */
@Component
public class AESEncrypt implements EncryptUtil {
//    @Autowired
//    AesUtils aesUtil;
    @Value("${encryption.prefix}")
    private String encryptionPrefix;
    /**
     * 加密
     *
     * @param declaredFields paramsObject所声明的字段
     * @param paramsObject   mapper中paramsType的实例
     * @return T
     * @throws IllegalAccessException 字段不可访问异常
     */
    @Override
    public <T> T encrypt(Field[] declaredFields, T paramsObject) throws IllegalAccessException {

        for (Field field : declaredFields) {
            //取出所有被EncryptDecryptField注解的字段
            SensitiveField sensitiveField = field.getAnnotation(SensitiveField.class);
            if (!Objects.isNull(sensitiveField)) {
                field.setAccessible(true);
                Object object = field.get(paramsObject);
                //暂时只实现String类型的加密
                if (object instanceof String) {
                    String value = (String) object;
                    //加密  这里我使用自定义的AES加密工具   同时加上加密头部
                    //TODO 加密
//                    field.set(paramsObject, aesUtil.encryptvalue());
                }
            }
        }
        return paramsObject;
    }
}
